package com.wetao.module_report;

import android.graphics.Color;
import android.util.Log;
import android.view.View;
import android.widget.DatePicker;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.alibaba.android.arouter.facade.annotation.Autowired;
import com.alibaba.android.arouter.facade.annotation.Route;
import com.wetao.lib_common.ConstantArouter;
import com.wetao.lib_common.base.BaseActivity;
import com.wetao.lib_common.bean.response.ReportStudyInfo;
import com.wetao.lib_common.tsdBlePen.TsdService;
import com.wetao.lib_common.utils.ComUtil;
import com.wetao.lib_common.utils.LogUtils;
import com.wetao.lib_common.utils.ToastUtil;
import com.wetao.lib_common.view.HorizontalChartView.Chart;
import com.wetao.lib_common.view.HorizontalChartView.HorizontalChartView;
import com.wetao.lib_common.view.LineChartView;
import com.wetao.lib_common.view.date.CustomDatePickerDialog;
import com.wetao.lib_common.view.date.Week;
import com.wetao.lib_common.view.date.WeekPickerDialog;
import com.wetao.module_report.contract.ReportStageDetailCon;
import com.wetao.module_report.dialog.DateDialogFragment;
import com.wetao.module_report.presenter.ReportStageDetailPre;

import org.joda.time.LocalDate;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

@Route(path = ConstantArouter.PATH_REPORT_STAGE_DETAIL_ACTIVITY)
public class ReportStageDetailActivity extends BaseActivity<ReportStageDetailCon.View, ReportStageDetailPre>
        implements ReportStageDetailCon.View, CustomDatePickerDialog.OnDateSetListener,WeekPickerDialog.OnWeekSelectListener {
    public static final int STAGE_TYPE_DAY = 1; //日报
    public static final String STAGE_TYPE_DAY_STR = "日报"; //日报
    public static final int STAGE_TYPE_WEEK = 2; //周报
    public static final String STAGE_TYPE_WEEK_STR = "周报"; //周报
    public static final int STAGE_TYPE_MONTH = 3; //月报
    public static final String STAGE_TYPE_MONTH_STR = "月报"; //月报

    @Autowired
    int type; //1:日报， 2：周报 3：月报
    @Autowired
    String typeStr; //显示文字：日报，周报，月报
    @Autowired
    int subjectId; //学科id
    @Autowired
    String subjectName;//学科名字

    @BindView(R2.id.tv_title)
    TextView tv_title;
    @BindView(R2.id.sl_reportContent)
    ScrollView sl_reportContent;
    @BindView(R2.id.ll_noData)
    LinearLayout ll_noData;
    @BindView(R2.id.tv_no_data)
    TextView tv_no_data;
    @BindView(R2.id.ll_line_chart)
    LinearLayout ll_lineChart;
    @BindView(R2.id.tv_examName)
    TextView tv_examName;
    @BindView(R2.id.tv_commitCount)
    TextView tv_commitCount;
    @BindView(R2.id.tv_myRate)
    TextView tv_myRate;
    @BindView(R2.id.tv_totalScore)
    TextView tv_totalScore;
    @BindView(R2.id.tv_classAvRate)
    TextView tv_classAvRate;
    @BindView(R2.id.tv_class_av_tag)
    TextView tv_class_av_tag;
    @BindView(R2.id.tv_ranking)
    TextView tv_ranking;
    @BindView(R2.id.tv_previous_ranking)
    TextView tv_previous_ranking;
    @BindView(R2.id.tv_tiliang)
    TextView tv_tiliang;
    @BindView(R2.id.tv_dadui)
    TextView tv_dadui;
    @BindView(R2.id.tv_dacuo)
    TextView tv_dacuo;
    @BindView(R2.id.tv_unanswerCount)
    TextView tv_unanswerCount;
    @BindView(R2.id.tv_uncorrectCount)
    TextView tv_uncorrectCount;
    @BindView(R2.id.tv_my_fen)
    TextView tv_my_fen;
    @BindView(R2.id.tv_my_miao)
    TextView tv_my_miao;
    @BindView(R2.id.tv_class_fen)
    TextView tv_class_fen;
    @BindView(R2.id.tv_class_miao)
    TextView tv_class_miao;
    @BindView(R2.id.ch_view)
    HorizontalChartView ch_view;
    @BindView(R2.id.tv_jy1)
    TextView tv_jy1;
    @BindView(R2.id.iv_jy1)
    ImageView iv_jy1;
    @BindView(R2.id.tv_jy1_tag)
    TextView tv_jy1_tag;
    @BindView(R2.id.tv_jy2_jiao)
    TextView tv_jy2_jiao;
    @BindView(R2.id.tv_jy2)
    TextView tv_jy2;
    @BindView(R2.id.iv_jy2)
    ImageView iv_jy2;
    @BindView(R2.id.tv_jy2_tag)
    TextView tv_jy2_tag;
    @BindView(R2.id.tv_jy3)
    TextView tv_jy3;
    @BindView(R2.id.iv_jy3)
    ImageView iv_jy3;
    @BindView(R2.id.tv_jy3_tag)
    TextView tv_jy3_tag;
    @BindView(R2.id.tv_jy4)
    TextView tv_jy4;
    @BindView(R2.id.iv_jy4)
    ImageView iv_jy4;
    @BindView(R2.id.tv_jy4_tag)
    TextView tv_jy4_tag;
    @BindView(R2.id.ll_line_view)
    LinearLayout ll_line_view;
    @BindView(R2.id.iv_report_arrow_left)
    ImageView iv_arrow_left;
    @BindView(R2.id.iv_report_arrow_right)
    ImageView iv_arrow_right;
    @BindView(R2.id.tv_date_text)
    TextView tv_date_text;


    ReportStudyInfo.DataBean info;

    private static final String SplitSymbol = " ~ ";
    private static final long M_Week_Day_MillIS = 7 * 24 * 60 * 60 * 1000; //一周
    private static final long M_Day_MillIS = 24 * 60 * 60 * 1000; //一天
    private Calendar calendar;
    private Calendar curCalendar; //当前设置的日历时间
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd");
    private SimpleDateFormat sdf_normal = new SimpleDateFormat("yyyy-MM-dd");
    private String curStr;  //当前文本 日周月
    private String curStr_shang;
    private String curStr_jiao_shang;
    private Date startDate;
    private Date endDate;

    private long startTime; //本周开始时间
    private long endTime;   //本周结束时间
    private long curWeekEndTime; //当前选中结束时间


    @Override
    public int getLayoutId() {
        return R.layout.report_stage_detail_activity;
    }

    @Override
    public boolean getIsBindTsdService() {
        return false;
    }

    @Override
    public TsdService.OnDataReceiveListener getTsdServiceDataReceiveListener() {
        return null;
    }

    @Override
    public ReportStageDetailPre createPresenter() {
        return new ReportStageDetailPre(this);
    }

    @Override
    public ReportStageDetailCon.View createView() {
        return this;
    }

    @Override
    public void init() {
        calendar = Calendar.getInstance();
        curCalendar = Calendar.getInstance();
        LogUtils.i("-->type=" + type + ", typeStr=" + typeStr + ", subjectId=" + subjectId + ", subjectName=" + subjectName);
        tv_title.setText(subjectName + "学情-" + typeStr);
        iv_arrow_right.setEnabled(false);
        initTvDate(type);

        ch_view.setMaxScore(100);

        getDateByType(type);
        //refreshUI();
    }


    /**
     * 根据type获取对应数据请求
     */
    private void getDateByType(int type) {
        String text = tv_date_text.getText().toString();
        String param;
        if (text.contains("（")) {
            param = text.substring(0, text.indexOf("（")).replace("/", "-");
        } else {
            param = text.replace("/", "-");
        }
        switch (type) {
            case 1:
                getDateStudyInfoReq(param);
                break;
            case 2:
                String[] dates = param.split(SplitSymbol);
                if (dates != null && dates.length == 2) {
                    String startDate = dates[0].replace("/", "-");
                    String endDate = dates[1].replace("/", "-");
                    getWeekStudyInfoReq(startDate, endDate);
                }
                break;
            case 3:
                getMonthStudyInfoReq(param);
                break;
        }
    }


    /**
     * 设置日期
     */
    private void initTvDate(int type) {
        ll_lineChart.setVisibility(View.VISIBLE);
        switch (type) {
            case 1:
                curStr = "今日";
                curStr_shang = "上一日排名";
                curStr_jiao_shang = "较上一日";
                String text = sdf.format(new Date());
                tv_date_text.setText(text + "（" + curStr + "）");
                ll_lineChart.setVisibility(View.GONE);
                tv_no_data.setText("本日无学情~");
                break;
            case 2:
                curStr = "本周";
                curStr_shang = "上一周排名";
                curStr_jiao_shang = "较上一周";
                startTime = ComUtil.getTimeOfWeekStart();
                endTime = startTime + 6 * M_Day_MillIS;
                curWeekEndTime = endTime;
                tv_date_text.setText(sdf.format(startTime) + SplitSymbol + sdf.format(endTime) + "（" + curStr + "）");
                tv_no_data.setText("本周无学情~");
                break;
            case 3:
                curStr = "本月";
                curStr_shang = "上一月排名";
                curStr_jiao_shang = "较上一月";
                text = sdf.format(curCalendar.getTime());
                text = text.substring(0, text.lastIndexOf("/"));
                tv_date_text.setText(text + "（" + curStr + "）");
                tv_no_data.setText("本月无学情~");
                break;
        }
    }


    /**
     * 日报表请求
     */
    private void getDateStudyInfoReq(String date) {
        getPresenter().queryDateStudyInfo(ComUtil.getHashmapByStr(
                new String[]{"subjectId","date"},
                new String[]{String.valueOf(subjectId),date}),
                false, false);
    }

    /**
     * 周报表请求
     */
    private void getWeekStudyInfoReq(String startDate, String endDate) {
        getPresenter().queryWeekStudyInfo(ComUtil.getHashmapByStr(
                new String[]{"subjectId","startDate", "endDate"},
                new String[]{String.valueOf(subjectId),startDate, endDate}),
                false, false);
    }

    /**
     * 月报表请求
     */
    private void getMonthStudyInfoReq(String month) {
        getPresenter().queryMonthStudyInfo(ComUtil.getHashmapByStr(
                new String[]{"subjectId","month"},
                new String[]{String.valueOf(subjectId),month}),
                false, false);
    }


    @OnClick({R2.id.iv_back, R2.id.iv_report_arrow_left, R2.id.iv_report_arrow_right, R2.id.tv_date_text
    })
    public void onClickView(View v) {
        int id = v.getId();
        if (id == R.id.iv_back) {
            finish();
        } else if (id == R.id.iv_report_arrow_left) {
            iv_arrow_right.setEnabled(true);
            updateDate(type, -1);
        } else if (id == R.id.iv_report_arrow_right) {
            updateDate(type, 1);
        } else if (id == R.id.tv_date_text) {
            showDateSelectDialog(type);
        }
    }


    /**
     * 选择日期弹框
     */
    private void showDatePickerDialog(boolean hideDate) {
        CustomDatePickerDialog datePickerDialog = new CustomDatePickerDialog(this, R.style.DateDialog,hideDate);
        datePickerDialog.setOnDateSelectedListener(this);
        datePickerDialog.show();
    }

    private void showWeekPickerDialog(){
        WeekPickerDialog weekPickerDialog = new WeekPickerDialog(this, Calendar.getInstance(), this);
        weekPickerDialog.show();
    }


    /**
     * 显示时间弹框
     *
     * @param type
     */
    private void showDateSelectDialog(int type) {
        switch (type){
            case 1:
                showDatePickerDialog(false);
                break;
            case 2:
                showWeekPickerDialog();
                break;
            case 3:
                showDatePickerDialog(true);
                break;
        }

    }

    /**
     * 更新时间
     *
     * @param type
     * @param change -1 表示向前 1表示向后  其余表示下拉选择
     */
    private void updateDate(int type, int change) {

        String text = "";
        int result = 0;
        switch (type) {
            case 1://日报表
                curCalendar.set(Calendar.DATE, curCalendar.get(Calendar.DATE) + change);
                text = sdf.format(curCalendar.getTime());
                result = overTime(curCalendar.get(Calendar.YEAR),
                        curCalendar.get(Calendar.MONTH),
                        curCalendar.get(Calendar.DAY_OF_MONTH), false);
                if (result == 0) iv_arrow_right.setEnabled(false);
                tv_date_text.setText(text + (result == 0 ? "（今日）" : ""));
                break;
            case 2: //周报表
                if (change > 0) {
                    startTime = curWeekEndTime + M_Day_MillIS;
                } else {
                    startTime = startTime + (change) * M_Week_Day_MillIS;
                }
                curWeekEndTime = startTime + 6 * M_Day_MillIS;
                text = sdf.format(startTime) + SplitSymbol + sdf.format(curWeekEndTime);
                long temp = ComUtil.getTimeOfWeekStart();
                if (temp == startTime){
                    iv_arrow_right.setEnabled(false);
                    tv_date_text.setText(text + (startTime == temp ? "（本周）" : ""));
                }else{
                    iv_arrow_right.setEnabled(true);
                    tv_date_text.setText(text);
                }
                break;
            case 3://月报表
                curCalendar.set(Calendar.MONTH, curCalendar.get(Calendar.MONTH) + change);
                text = sdf.format(curCalendar.getTime());
                result = overTime(curCalendar.get(Calendar.YEAR),
                        curCalendar.get(Calendar.MONTH),
                        curCalendar.get(Calendar.DAY_OF_MONTH), true);
                if (result == 0) iv_arrow_right.setEnabled(false);
                text = text.substring(0, text.lastIndexOf("/"));
                tv_date_text.setText(text + (result == 0 ? "（本月）" : ""));
                break;
        }

        getDateByType(type);
    }

    public List<Chart> getCharts() {
        List<Chart> list = new ArrayList<>();
        Chart chart = new Chart();
        chart.setLeftName("错题率");
        chart.setRightName(float2float(info == null || info.getWrongRatio() == null ? 0 : info.getWrongRatio().floatValue()));
        Chart chart1 = new Chart();
        chart1.setLeftName("班级平均错题率");
        chart1.setRightName(float2float(info == null || info.getClassWrongRatio() == null ? 0 : info.getClassWrongRatio().floatValue()));
        list.add(chart1);
        list.add(chart);
        return list;
    }


    private void initLineView() {
        LineChartView lineChartView = new LineChartView(this);
        List<String> dateList = new ArrayList<>();
        List<Float> minRatioData = new ArrayList<>();
        List<Float> classRatioData = new ArrayList<>();
        if (info != null) {
            dateList.clear();
            minRatioData.clear();
            classRatioData.clear();
            dateList.addAll(info.getDateList());
            minRatioData.addAll(info.getMineRatioList());
            classRatioData.addAll(info.getClassRatioList());
        }
        //X轴
        ArrayList xItemArray = new ArrayList();
        for (int i = 0; i < dateList.size(); i++) {
            xItemArray.add(dateList.get(i));
        }
        //Y轴

        ArrayList<Float> yItemArray = new ArrayList<>();
        ArrayList<String> labels = new ArrayList<>();
        labels.add("我的");
        labels.add("班级平均");
        for (int i = 0; i < minRatioData.size(); i++) {
            yItemArray.add(minRatioData.get(i) == null ? 0 : minRatioData.get(i) * 100);
        }
        ArrayList<Float> yItemArray1 = new ArrayList<>();
        for (int i = 0; i < classRatioData.size(); i++) {
//            yItemArray1.add((float) (Math.random() * 90 + 1));
            yItemArray1.add(classRatioData.get(i) == null ? 0 : classRatioData.get(i) * 100);
        }
        ArrayList<ArrayList<Float>> data = new ArrayList<>();
        data.add(yItemArray);
        data.add(yItemArray1);
        lineChartView.setXItem(xItemArray);
        lineChartView.setYItem(data);
        lineChartView.setMaxYValue(100);
        lineChartView.setLabel(labels);
        ll_line_view.removeAllViews();
        ll_line_view.addView(lineChartView);

    }

    private String float2Str(Float f) {
        if(f == null) {
            return "0";
        }
        BigDecimal a= new BigDecimal(f);
        BigDecimal j = a.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return j.stripTrailingZeros().toPlainString();

        /*int i = (int)(f.floatValue() * 10000);
        int i_a = i / 100;
        int i_b = i % 100;
        if(i_b == 0) {
            return i_a + "";
        } else {
            return  i_a + "." + i_b;
        }*/
    }

    private float float2float(Float f) {
        if(f == null) {
            return 0;
        }

        BigDecimal a= new BigDecimal(f);
        BigDecimal j = a.multiply(new BigDecimal(100)).setScale(2, BigDecimal.ROUND_HALF_UP);
        return j.stripTrailingZeros().floatValue();
    }

    private void refreshUI() {
        //tv_examName.setText("作业名称：" + info.getExamName());
        tv_commitCount.setText(info.getCommitCount() == null ? "0" : String.valueOf(info.getCommitCount()));
        tv_myRate.setText(float2Str(info.getScoreRatio()) +"%");
        if (info.getScoreRatio().floatValue() >= info.getClassScoreAverageRatio().floatValue()) {
            tv_myRate.setTextColor(Color.parseColor("#ff00a8b2"));
        } else {
            tv_myRate.setTextColor(Color.parseColor("#FF3A51"));
        }
        tv_totalScore.setText(curStr + "得分率");
        tv_classAvRate.setText(float2Str(info.getClassScoreAverageRatio().floatValue()) +"%");
        tv_class_av_tag.setText(curStr + "班级平均得分率");
        tv_ranking.setText(info.getClassRank() + "");
        if (info.getPreviousRank() != null && info.getClassRank().intValue() > info.getPreviousRank().intValue()) {
            tv_ranking.setTextColor(Color.parseColor("#FF3A51"));
        } else {
            tv_ranking.setTextColor(Color.parseColor("#ff00a8b2"));
        }
        if (info.getPreviousRank() != null) {
            tv_previous_ranking.setText("（" + curStr_shang + info.getPreviousRank().intValue() + "）");
        } else {
            tv_previous_ranking.setText("（"+ curStr_shang +"-）");
        }
        tv_tiliang.setText(info.getQuestionCount() + "");
        tv_dadui.setText(info.getRightCount() + "");
        tv_dacuo.setText(info.getWrongCount() + "");
        tv_unanswerCount.setText(info.getUnanswerCount() + "");
        tv_uncorrectCount.setText(info.getUncorrectCount() + "");
        tv_my_fen.setText(info.getAnswerTime().intValue() / 60 + "");
        tv_my_miao.setText(info.getAnswerTime().intValue() % 60 + "");
        tv_class_fen.setText(info.getClassAnswerTime().intValue() / 60 + "");
        tv_class_miao.setText(info.getClassAnswerTime().intValue() % 60 + "");
        ch_view.setMaxScore(100);
        ch_view.setData(getCharts());
        float diff;
        String diff_str;
        int resId;
        String diff_tag;
        //diff = ((int)((info.getScoreRatio().floatValue() - info.getClassScoreAverageRatio().floatValue()) * 10000))/100f;
        diff = float2float(info.getScoreRatio().floatValue() - info.getClassScoreAverageRatio().floatValue());
        if (diff > 0) {
            diff_str = "高" + diff + "%";
            tv_jy1.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else if (diff < 0) {
            diff_str = "低" + (-diff)  + "%";
            tv_jy1.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else {
            diff_str = "-持平";
            tv_jy1.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy1.setText(diff_str);
        iv_jy1.setImageResource(resId);
        tv_jy1_tag.setText(diff_tag);

        if(info.getPreviousRank() == null) {
            diff = 0;
        } else {
            diff = info.getClassRank().intValue() - info.getPreviousRank().intValue();
        }
        if (diff > 0) {
            diff_str = "下降" + (int)diff + "名";
            tv_jy2.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else if (diff < 0) {
            diff_str = "上升" + (int)(-diff) + "名";
            tv_jy2.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else {
            diff_str = "-持平";
            tv_jy2.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy2.setText(diff_str);
        iv_jy2.setImageResource(resId);
        tv_jy2_tag.setText(diff_tag);
        tv_jy2_jiao.setText(curStr_jiao_shang);


        //diff = ((int)((info.getWrongRatio().floatValue() - info.getClassWrongRatio().floatValue()) * 10000)) / 100f;
        diff = float2float(info.getWrongRatio().floatValue() - info.getClassWrongRatio().floatValue());
        LogUtils.i("diff:" + diff);
        if(diff > 0) {
            diff_str = "高" + diff + "%";
            tv_jy3.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else if(diff < 0) {
            diff_str = "低" + (-diff) + "%";
            tv_jy3.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else {
            diff_str = "-持平";
            tv_jy3.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy3.setText(diff_str);
        iv_jy3.setImageResource(resId);
        tv_jy3_tag.setText(diff_tag);

        diff = info.getClassAnswerTime().intValue() - info.getAnswerTime().intValue();
        if(diff > 0) {
            int fen = (int)(diff/60);
            int miao = (int)(diff%60);
            diff_str = "快" + (fen > 0 ? (fen + "分") : "") + (miao > 0 ? (miao + "秒") : "");
            tv_jy4.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy1;
            diff_tag = "表现不错，继续保持！";
        } else if(diff < 0) {
            int fen = (int)(-diff/60);
            int miao = (int)(-diff%60);
            diff_str = "慢" + (fen > 0 ? (fen + "分") : "") + (miao > 0 ? (miao + "秒") : "");
            tv_jy4.setTextColor(Color.parseColor("#FF3A51"));
            resId = R.drawable.report_xqbg_xxjy3;
            diff_tag = "表现较差，努力加油！";
        } else {
            diff_str = "-持平";
            tv_jy4.setTextColor(Color.parseColor("#ff00a8b2"));
            resId = R.drawable.report_xqbg_xxjy2;
            diff_tag = "表现尚可，有待提升！";
        }
        tv_jy4.setText(diff_str);
        iv_jy4.setImageResource(resId);
        tv_jy4_tag.setText(diff_tag);
    }

    @Override
    public void queryDateStudyInfoResult(ReportStudyInfo response) {
        info = response.getData();
        setReportContentVisible_pre();
        refreshUI();
    }

    @Override
    public void queryDateStudyInfoFailed() {
        setReportContentVisible(false);
    }

    @Override
    public void queryWeekStudyInfoResult(ReportStudyInfo response) {
        info = response.getData();
        setReportContentVisible_pre();
        refreshUI();
        initLineView();
    }

    @Override
    public void queryWeekStudyInfoFailed() {
        setReportContentVisible(false);
    }

    @Override
    public void queryMonthStudyInfoResult(ReportStudyInfo response) {
        info = response.getData();
        setReportContentVisible_pre();
        refreshUI();
        initLineView();
    }

    @Override
    public void queryMonthStudyInfoFailed() {
        setReportContentVisible(false);
    }

    private void setReportContentVisible_pre() {
        if(info.getCommitCount().intValue() == 0) {
            setReportContentVisible(false);
        } else {
            setReportContentVisible(true);
        }
    }
    private void setReportContentVisible(boolean visible) {
        if (visible) {
            sl_reportContent.setVisibility(View.VISIBLE);
            ll_noData.setVisibility(View.GONE);
        } else {
            sl_reportContent.setVisibility(View.GONE);
            ll_noData.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 比较日期,是否超出限制  1 超过  0 相等 -1 小于
     */
    private int overTime(int year, int month, int day, boolean noDate) {
        int _year = calendar.get(Calendar.YEAR);
        int _month = calendar.get(Calendar.MONTH);
        int _date = calendar.get(Calendar.DAY_OF_MONTH);
        if (year == _year && month == _month && day == _date) {
            return 0;
        }
        if (year > _year) {
            return 1;
        } else if (year < _year) {
            return -1;
        } else {
            if (month > _month) {
                return 1;
            } else if (month < _month) {
                return -1;
            } else {
                if (noDate) return 0;
                if (day > _date) {
                    return 1;
                } else if (day < _date) {
                    return -1;
                } else {
                    return 0;
                }
            }
        }
    }


    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day) {

    }
    @Override
    public void onDateSet(DatePicker datePicker, int year, int month, int day,boolean hide) {
        int result = overTime(year, month,day, hide);
        if (result == 1) {
            ToastUtil.initToast("日期超过限制，请重新选择");
            return;
        }
        iv_arrow_right.setEnabled(result == 0 ? false : true);
        curCalendar.set(year, month,day);
        if (hide){
            String text = sdf.format(curCalendar.getTime());
            text = text.substring(0, text.lastIndexOf("/"));
            tv_date_text.setText(text + (result == 0 ? "（" + curStr + "）" : ""));
        }else {
            tv_date_text.setText(sdf.format(curCalendar.getTime()) + (result == 0 ? "（" + curStr + "）" : ""));
        }
        getDateByType(type);
    }

    @Override
    public void onWeekSelect(Week week) {
        startDate = week.getWeekBeginDate();
        endDate = week.getWeekEndDate();

        int result = 0;
        try {
            long _startTime = startDate.getTime();
            long _endTime = endDate.getTime();

            result = (int) ((endTime - _endTime) / 1000);
            LogUtils.i("m1:" + "endTime:" + endTime + ",_endTime:" + _endTime + ",result:" + result) ;
            if (result < 0){
                ToastUtil.initToast("日期超过限制，请重新选择");
                return;
            }
            startTime = _startTime;
            curWeekEndTime = _endTime;
        } catch (Exception e) {
            e.printStackTrace();
        }

        if (result == 0) {
            iv_arrow_right.setEnabled(false);
        }else{
            iv_arrow_right.setEnabled(true);
        }

        tv_date_text.setText(week.getSelectWeekBeginAndEnd() + (result==0?"（" + curStr + "）" : ""));
        getDateByType(type);
    }
}
